package com.group1.onlineeducation.sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.group1.onlineeducation.sugar.bean.*;
import com.group1.onlineeducation.sugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class SugarController {

    @Autowired
    TrafficService trafficService;

    @RequestMapping("/sugar/scTraffic")
    public String scTraffic(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<ScTraffic> scTraffic = trafficService.scTraffic(date);

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();

        JSONArray categories = new JSONArray();
        for (ScTraffic sc : scTraffic) {
            categories.add(sc.getSc());
        }
        data.put("categories", categories);

        JSONArray series = new JSONArray();
        JSONObject uv = new JSONObject();
        JSONObject sv = new JSONObject();
        JSONObject pv_sv_avg = new JSONObject();
        JSONObject dur_sv_avg = new JSONObject();
        JSONObject uj_rate = new JSONObject();


        JSONArray uvData = new JSONArray();
        JSONArray svData = new JSONArray();
        JSONArray pv_sv_avgData = new JSONArray();
        JSONArray dur_sv_avgData = new JSONArray();
        JSONArray uj_rateData = new JSONArray();

        for (ScTraffic sc : scTraffic) {
            uvData.add(sc.getUv());
            svData.add(sc.getSv());
            pv_sv_avgData.add(sc.getPv_sv_avg());
            dur_sv_avgData.add(sc.getDur_sv_avg());
            uj_rateData.add(sc.getUj_rate() );
        }

        uv.put("name","uv");
        uv.put("data",uvData);
        series.add(uv);

        sv.put("name","sv");
        sv.put("data",svData);
        series.add(sv);

        pv_sv_avg.put("name","pvAvg");
        pv_sv_avg.put("data",pv_sv_avgData);
        series.add(pv_sv_avg);

        dur_sv_avg.put("name","durAvg");
        dur_sv_avg.put("data",dur_sv_avgData);
        series.add(dur_sv_avg);

        uj_rate.put("name","ujRate");
        uj_rate.put("data",uj_rateData);
        series.add(uj_rate);

        data.put("series",series);
        result.put("data",data);
        return result.toJSONString();
    }

    @RequestMapping("/sugar/hourTraffic")
    public String hourTraffic(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        Map<Integer,HourTraffic> hourTrafficMap = new HashMap<>();
        List<HourTraffic> list = trafficService.hourTraffic(date);
        // 把list转成map, 方便后面能够根据hour来获取对应的值
        // key: hour, value: Traffic
        for (HourTraffic traffic : list) {
            hourTrafficMap.put(traffic.getHour(), traffic);
        }

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();

        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();

        for (int hour = 0; hour < 24; hour++) {
            categories.add(hour);
        }
        data.put("categories", categories);

        JSONObject pv = new JSONObject();
        JSONObject uv = new JSONObject();
        JSONObject pvSvAvg = new JSONObject();

        JSONArray pvData = new JSONArray();
        JSONArray uvData = new JSONArray();
        JSONArray pvSvAvgData = new JSONArray();

        pv.put("name", "pv");
        pv.put("data", pvData);
        series.add(pv);

        uv.put("name", "uv");
        uv.put("data", uvData);
        series.add(uv);

        pvSvAvg.put("name", "pvSvAvg");
        pvSvAvg.put("data", pvSvAvgData);
        series.add(pvSvAvg);

        for (int hour = 0; hour < 24; hour++) {
            // 根据hour来获取pv的值
            HourTraffic traffic = hourTrafficMap.getOrDefault(hour, new HourTraffic(hour, 0L, 0L, 0.0));
            pvData.add(traffic.getPv());
            uvData.add(traffic.getUv());
            pvSvAvgData.add(traffic.getPvSvAvg());
        }


        data.put("series", series);
        result.put("data", data);
        return result.toJSONString();
    }


    @RequestMapping("/sugar/kwTraffic")
    public String kwTraffic(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<KwTraffic> kwTraffic = trafficService.kwTraffic(date);

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONArray data = new JSONArray();

        for (KwTraffic kw : kwTraffic) {
            JSONObject obj = new JSONObject();

            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getScore());

            data.add(obj);
        }

        result.put("data", data);
        return result.toJSONString();
    }

    @RequestMapping("/sugar/provinceTrade")
    public String provinceTrade(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<ProvinceTrade> provinceTrade = trafficService.provinceTrade(date);

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");


        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();

        for (ProvinceTrade province : provinceTrade) {
            JSONObject obj = new JSONObject();
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());

            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_num_ct());
            tooltipValues.add(province.getOrder_time_ct());
            obj.put("tooltipValues", tooltipValues);
            mapData.add(obj);
        }
        data.put("mapData",mapData);

        data.put("valueName", "销售额");

        JSONArray tooltipNames = new JSONArray();

        tooltipNames.add("下单人数");
        tooltipNames.add("下单人次");

        data.put("tooltipNames", tooltipNames);


        JSONArray tooltipUnits = new JSONArray();

        tooltipUnits.add("人");
        tooltipUnits.add("次");

        data.put("tooltipUnits", tooltipUnits);

        result.put("data", data);
        return result.toJSONString();
    }

}
